package com.ruoyi.filemanager.command;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.filemanager.constant.ElFinderConstants;
import com.ruoyi.filemanager.service.ElfinderStorage;
import com.ruoyi.filemanager.service.VolumeHandler;
import com.ruoyi.filemanager.util.HashesUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MkdirCommand extends AbstractJsonCommand implements ElfinderCommand
{
    @Override
    protected void execute(ElfinderStorage elfinderStorage, HttpServletRequest request, JSONObject json)
            throws Exception
    {
        String target = request.getParameter(ElFinderConstants.ELFINDER_PARAMETER_TARGET);
        String dirName = request.getParameter(ElFinderConstants.ELFINDER_PARAMETER_NAME);

        if (dirName == null)
        {
            String[] paramMap = request.getParameterMap().get(ElFinderConstants.ELFINDER_PARAMETER_DIRS);
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> hashes = new HashMap<>();
            for (String dir : paramMap)
            {
                VolumeHandler volumeHandler = findTarget(elfinderStorage, target);
                VolumeHandler directory = new VolumeHandler(volumeHandler, dir);
                directory.createFolder();
                list.add(getTargetInfo(request, directory));
                hashes.put(dir, target + HashesUtil.encode(dir));
            }
            json.put(ElFinderConstants.ELFINDER_JSON_RESPONSE_ADDED, list);
            json.put(ElFinderConstants.ELFINDER_JSON_RESPONSE_HASHES, hashes);
        }
        else
        {
            VolumeHandler volumeHandler = findTarget(elfinderStorage, target);
            VolumeHandler directory = new VolumeHandler(volumeHandler, dirName);
            directory.createFolder();

            json.put(ElFinderConstants.ELFINDER_JSON_RESPONSE_ADDED,
                    new Object[] { getTargetInfo(request, directory) });
        }

    }
}
